/**
 * WebSocket API接口
 * 用于检查连接状态、获取连接URL等
 */

export interface WebSocketStatus {
  userId: string
  connected: boolean
  sessionInfo: string
  totalConnections: number
}

export interface WebSocketUrl {
  userId: string
  wsUrl: string
  protocol: string
  endpoint: string
}

export interface WebSocketConnections {
  totalConnections: number
  activeSessions: Record<string, string>
}

/**
 * 检查用户WebSocket连接状态
 */
export async function checkWebSocketStatus(userId: string): Promise<WebSocketStatus> {
  const response = await fetch(`/api/websocket/status/${userId}`)
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`)
  }
  const result = await response.json()
  return result.data
}

/**
 * 获取用户WebSocket连接URL
 */
export async function getWebSocketUrl(userId: string): Promise<WebSocketUrl> {
  const response = await fetch(`/api/websocket/url/${userId}`)
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`)
  }
  const result = await response.json()
  return result.data
}

/**
 * 获取所有活跃连接
 */
export async function getAllConnections(): Promise<WebSocketConnections> {
  const response = await fetch('/api/websocket/connections')
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`)
  }
  const result = await response.json()
  return result.data
}
